﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Facebook;

namespace lydoe
{
    public partial class _Default : System.Web.UI.Page
    {
        private string PageId = "332778643418527";//vlhanoi.com.vn
        private string sLink = "http://www.facebook.com/vlhanoi.com.vn";
        protected void Page_Load(object sender, EventArgs e)
        {
            string host = Request.ServerVariables["HTTP_HOST"];
            if (Request.QueryString["code"] != null)
            {
                var cl = new FacebookOAuthClient();
                cl.AppId = "277864275663763";
                cl.AppSecret = "4ca053e90377f12d9272183137921bcc";
                cl.RedirectUri = new Uri("http://" + host + "/lydoe/default.aspx");

                var code = (IDictionary<string, object>)cl.ExchangeCodeForAccessToken(Request.QueryString["code"]);

                var fb = new Facebook.FacebookClient((string)code["access_token"]);

                if (!facebookInt.GetLikePage(fb, PageId))
                {
                    return;
                }

                var me = (IDictionary<string, object>)fb.Get("/me");
                //bool isGirl =(string)me["gender"] == "female";
                Random rnd = new Random();
                int n = rnd.Next(1, 20);
                string sbg = "/lydoe/images/" + n.ToString() + ".jpg";
                sbg = Server.MapPath(sbg);
                var path = ImageHandle.ImageOver("https://graph.facebook.com/" + (string)me["id"] + "/picture?type=large", (string)me["id"], sbg, Server.MapPath("/lydoe/out"), 195, 168, 100, 100);

                var ex = new Dictionary<string, object>();
                var facebookMedia = new FacebookMediaObject
                {
                    ContentType = "image/jpg",
                    FileName = path
                };

                //var tags = new[] 
                //{ 
                //    new { tag_uid = friendId_1, x = 20, y = 20 },
                //    new { tag_uid = friendId_2, x = 40, y = 40 },
                //    new { tag_uid = (string)me.id, x = 60, y = 60 }
                //};

                var bytes = File.ReadAllBytes(path);
                facebookMedia.SetValue(bytes);
                ex["source"] = facebookMedia;
                //ex.tags = tags;
                ex["message"] = "lý do vì sao bạn ế..." + @"http://www.facebook.com/apps/application.php?id=277864275663763";
                var result = fb.Post("me/photos", ex);
                Response.Write("<script type=\"text/javascript\">alert(\"Thành công rùi đó! Xem trên tường bạn nhé!\");window.location.href='" + sLink + "';</script>");

                //File.Delete(path);
                //var c = (JsonObject)fb.Get("me/posts");
                //var dn = (JsonObject)fb.Get("/me/friends?fields=id,name,birthday");
                //var listFriend = new List<User>();
                //var stb = new StringBuilder();
                //stb.AppendLine("Tổng số trong danh sách có " + ((JsonArray)dn["data"]).Count + "bạn!");
                //foreach (var friend in (JsonArray)dn["data"])
                //{
                //    // var user = new User();
                //    stb.AppendLine("ID: " + ((JsonObject)friend)["id"] + "\t Tên: " + ((JsonObject)friend)["name"] +
                //                   "\t Ngày sinh(nếu có): " + ((JsonObject)friend)["birthday"]);
                //    // listFriend.Add(user);
                //}
                //dynamic me = new ExpandoObject();
                //me.message = stb.ToString();
                //dynamic d = fb.Post("/me/feed", me);
                //File.WriteAllText("D:\\facebook.txt", c.ToString(), Encoding.UTF8);
            }
            else
            {
                string spath = @"https://graph.facebook.com/oauth/authorize?client_id=" +
                               "277864275663763" + "&redirect_uri=http://" + host +
                               @"/lydoe/default.aspx&scope=publish_stream";
                Response.Redirect(spath);

                //Response.Redirect(@"https://graph.facebook.com/oauth/authorize?client_id=" +
                //              "277864275663763" + "&redirect_uri=http://" + host +
                //              @"/default.aspx&scope=publish_stream,user_birthday,email");
            }
        }
    }
}
